package com.demo.springboot;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/***
 * registerBeanDefinitions的方法有一个参数可以用来往Spring容器中注入Bean
 * 如此，我们就可以在registerBeanDefinitions方法里面动态的注入Bean
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        BeanDefinitionBuilder userBDB = BeanDefinitionBuilder.rootBeanDefinition(User.class);
        registry.registerBeanDefinition("user",userBDB.getBeanDefinition());

        BeanDefinitionBuilder runnableBDB = BeanDefinitionBuilder.rootBeanDefinition(MyConfigration.class);
        registry.registerBeanDefinition(MyConfigration.class.getName(),runnableBDB.getBeanDefinition());
    }
}
